package com.platform.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.platform.base.PageUtil;
import com.platform.base.R;
import com.platform.base.mvc.BaseController;
import com.platform.dao.RoleRightDAO;
import com.platform.dao.UserRoleDAO;
import com.platform.dto.Role;
import com.platform.dto.RoleRight;
import com.platform.dto.UserRole;
import com.platform.logger.annotation.ApiLog;
import com.platform.service.IRole;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * <p> Title:Js Mapper</p>
 * <p> desc:  角色表</p>
 * <p> Copyright: Copyright (c) 2019 </p>
 * <p> Company: </p>
 *
 * @author wys
 * @version 1.0
 */
@Controller
@RequestMapping("/role") 
@Api(tags="OMS-角色定义模块")
public class RoleController extends BaseController{
	
	@Autowired
    private IRole services;
	
	@Autowired
	private RoleRightDAO<RoleRight> roleRightDAO;
	
	@Autowired
	private UserRoleDAO<UserRole> userRoleDAO;
    
    
    @RequestMapping(value="data",method=RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value="分页查询")
    public Object selectAll(Role dto,Integer start,Integer limit,HttpServletRequest request, HttpServletResponse response,
            HttpSession session) {
        PageUtil pageUt = new PageUtil();
        pageUt.setPageIndex(start);
        pageUt.setPageSize(limit);
        pageUt.setParams(BeanUtil.beanToMap(dto));
        List<Role> servicesList = services.selectByPaging(pageUt);
        return R.success(servicesList, pageUt.getTotal());
    }

    @ApiLog(value="角色数据删除",desc="系统角色数据删除")
    @SuppressWarnings("all")
    @RequestMapping(value="delete",method=RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value="删除数据")
    public Object delete(String ids,HttpServletRequest request, HttpServletResponse response,
            HttpSession session) throws IOException {
    	boolean isSuccess = false;
    	String[] codes = ids.split(",");
    	for(String pk : codes){
    		Role role = services.get(pk);
    		roleRightDAO.deleteByJsId(role.getId());
    		userRoleDAO.deleteByJsId(role.getId());
    		isSuccess = services.delete(role);
    	}
		return R.success();
    }
    
    @ApiLog(value="角色数据保存",desc="系统角色数据保存")
    @RequestMapping(value="save",method=RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value="新增数据")
    public Object insert(@Valid Role dto,HttpServletRequest request, HttpServletResponse response,
            HttpSession session) {
    	dto.setCreateDate(new Date(System.currentTimeMillis()));
    	dto.setCreater(getName());
    	if(services.insert(dto)) {
    		return R.success();
    	}else {
    		return R.fail();
    	}
    }
    
    @ApiLog(value="角色数据修改",desc="系统角色数据修改")
    @RequestMapping(value="edit",method=RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value="修改数据")
    public Object update(@Valid Role dto,HttpServletRequest request, HttpServletResponse response,
            HttpSession session) {
    	dto.setLastModifier(getName());
    	dto.setLastModifyDate(new Date(System.currentTimeMillis()));
    	if(services.update(dto)){
        	return R.success();
    	}else{
    		return R.fail();
    	}
    }
    
    @RequestMapping(value="find",method=RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value="获取单条数据")
    public Object get(String code, HttpServletRequest request, HttpServletResponse response,
            HttpSession session) {
        return services.get(code);
    }
}
